Skip to content

获取前景点 - GetForegroundPoints

函数简介

获取二值化图像中的前景点坐标信息,以JSON格式返回。

接口名称

GetForegroundPoints

DLL调用

long GetForegroundPoints(long instance, long ptr);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
ptr长整数型图像指针

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
    auto points = ola.GetForegroundPoints(imgPtr);
    if (!points.empty()) {
        int x = points[0].X;
        int y = points[0].Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0)
{
    var points = ola.GetForegroundPoints(imgPtr);
    if (points.Count > 0)
    {
        int x = points[0].X;
        int y = points[0].Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
img_ptr = ola.LoadImage("images/scene.png")
if img_ptr:
    points = ola.GetForegroundPoints(imgPtr)
    if points:
        x = points[0].get("X")
        y = points[0].get("Y")
    # 后续不再使用该图时释放
    ola.FreeImage(img_ptr)
java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.ApiResult;
import com.olaplug.model.Point;
import com.olaplug.model.Size;
import com.olaplug.model.Rect;
import java.util.List;

OLAPlugServer ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
    List<Point> points = ola.GetForegroundPoints(imgPtr);
    if (points != null && !points.isEmpty()) {
        int x = points.get(0).X;
        int y = points.get(0).Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
cpp
var ola = com("OlaPlug.OlaSoft")
var imgPtr = ola.LoadImage("images/scene.png")
if(imgPtr) {
    var points = ola.GetForegroundPoints(imgPtr)
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
imgPtr = ola.LoadImage("images/scene.png")
If imgPtr <> 0 Then
    points = ola.GetForegroundPoints(imgPtr)
    ' 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
End If
text
.局部变量 ola, OLAPlug
ola.创建 ()
imgPtr = ola.LoadImage ("images/scene.png")
.如果真 (imgPtr ≠ 0)
    points = ola.GetForegroundPoints(imgPtr)
    ' 后续不再使用该图时释放
    ola.FreeImage (imgPtr)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var imgPtr = ola.LoadImage("images/scene.png");
if(imgPtr){
    var points = ola.GetForegroundPoints(imgPtr);
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 imgPtr = ola.LoadImage("images/scene.png")
如果真 (imgPtr ≠ 0)
{
    PointList points = ola.GetForegroundPoints(imgPtr)
    ' 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
    auto points = ola.GetForegroundPoints(imgPtr);
    if (!points.empty()) {
        int32_t x = points[0].X;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = GetForegroundPoints(instance, imgPtr);
if (ptr != 0) {
    char json[8192] = {0};
    GetStringFromPtr(ptr, json, sizeof(json));
    FreeStringPtr(ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long GetForegroundPoints(long ola, long ptr);

long instance = CreateCOLAPlugInterFace();
long ptr = GetForegroundPoints(instance, imgPtr);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string json = sb.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, byref, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.GetForegroundPoints(instance, ...)
# 原生返回 JSON 坐标数组指针

返回值

长整数型:返回前景点数组的JSON字符串指针,格式为 [{"x":10,"y":10},{"x":20,"y":20}]。失败返回0。

注意事项

  • 返回的字符串指针需调用 FreeStringPtr 释放内存。
  • 前景点检测基于图像的非零像素。
  • 建议与 Threshold 函数配合使用。